import '@/assets/mapbox/libs/draw/mapbox-gl-draw.js'
import * as turf from '@turf/turf';

export default class BoxUtil {

  /**
   * 获取面的中心的点
   * @param coordinates
   * @returns {LngLat}
   */
  static getRegionCenter(coordinates) {
    let b = new mapboxgl.LngLatBounds(coordinates[0], coordinates[0]);
    coordinates.reduce(function (bounds, coord) {
      return bounds.extend(coord);
    }, b);
    return b.getCenter();
  }


  /**
   * 获取点串的边界范围
   * @param coordinates
   * @returns {*}
   */
  static getCoordinatesBounds(coordinates) {
    let b = new mapboxgl.LngLatBounds(coordinates[0], coordinates[0]);
    return coordinates.reduce(function (bounds, coord) {
      return bounds.extend(coord);
    }, b);
  }


  /**
   * 获取线的中心点
   * @param coordinates
   */
  static getCenterOfLine(coordinates) {
    var len = this.getKiloMiterLength(coordinates);
    return this.getKiloMitreLengthOnLine(coordinates, len / 2).geometry.coordinates;
  }

  /**
   * 某起始点开始n米出的坐标
   * @param coordinates
   * @param len
   * return []坐标
   */
  static getKiloMitreLengthOnLine(coordinates, len) {
    return turf.along(turf.lineString(coordinates), len, { units: 'kilometers' });
  }

  /**
   * 获取线长度（米）
   * @param coordinates
   * @returns {number}
   * return  0
   */
  static getKiloMiterLength(coordinates) {
    let line = turf.lineString(coordinates);
    return turf.length(line, { units: 'kilometers' });
  }

  /**
   * 获取线上的点
   * @param coordinates  线坐标
   * @param pointer      鼠标点
   * returns {Position}
   */
  static getCoordinateOnLine(coordinates, pointer) {
    let line = turf.lineString(coordinates);
    let pt = turf.point(pointer);
    return turf.nearestPointOnLine(line, pt, { units: 'kilometers' });

  }

  /**
   *
   * @param isPasstive
   * @param coordinates
   * @param pointer
   * @returns {number}
   */
  static getKiloLengthFromStart(isPasstive, end, coordinates) {
    let line = turf.lineString(coordinates);
    let l = turf.lineSlice(turf.point(isPasstive ? coordinates[0] : coordinates[coordinates.length - 1]), end, line);
    return turf.length(l, { units: 'kilometers' });
  }


  static getFeatureBounds(geometry) {
    let bbBox = turf.bbox(geometry);
    return bbBox;
  }

  static ploygon2Line(geometry) {
    var poly = turf.polygon(geometry.coordinates);

    return turf.polygonToLine(poly);
  }



}

